import functools

from flask import session, g, jsonify

from ihome.models import User
from ihome.utils.response_code import RET


def check_login(f):
    # 验证用户是否登录
    @functools.wraps(f)
    def n(*args, **kwargs):
        user_id = session.get("user_id")
        if user_id:
            g.user_id = user_id
            return f(*args, **kwargs)
        else:
            return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录")
    return n
